CREATE PROCEDURE LX_PROCESSOS_VERIFICA_ERRO      
AS      
DECLARE @ID INT,@COMANDO VARCHAR(1000),@STATUS TINYINT, @DESC_STATUS VARCHAR(500) , @ERRO INT      
      
SET NOCOUNT ON      
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'LX_PROCESSO_LOG')      
IF EXISTS (SELECT * FROM LX_PROCESSO_LOG WHERE STATUS=0 OR STATUS=10)      
BEGIN      
 DECLARE CUR_PROCESSO CURSOR FOR      
  SELECT SEQUENCIA,COMANDO       
  FROM LX_PROCESSO_LOG       
  WHERE (STATUS=0 OR STATUS=10)      
--   AND DATA_LOG <= DATEADD(MI,-1,GETDATE())      
      
 OPEN CUR_PROCESSO      
 FETCH NEXT FROM CUR_PROCESSO INTO @ID, @COMANDO      
 WHILE @@FETCH_STATUS=0      
 BEGIN      
  IF EXISTS (SELECT * FROM LX_PROCESSO_LOG WHERE SEQUENCIA=@ID AND (STATUS=0 OR STATUS=10))      
  BEGIN      
   PRINT @ID    
   EXECUTE(@COMANDO)      
   SELECT @ERRO=@@ERROR      
   IF @ERRO=0      
    SELECT @STATUS=1, @DESC_STATUS='PROCESSADO SEM ERROS'      
   ELSE      
    SELECT @STATUS=9, @DESC_STATUS='OCORREU UM ERRO DURANTE O PROCESSO ! (ERRO='+STR(@ERRO)+')'      
   UPDATE LX_PROCESSO_LOG SET STATUS=@STATUS, LOG_PROCESSO=@DESC_STATUS, DATA_PROCESSADO=GETDATE()      
   WHERE COMANDO=@COMANDO AND STATUS=0 OR STATUS=10      
  END      
  FETCH NEXT FROM CUR_PROCESSO INTO @ID, @COMANDO      
 END      
 CLOSE CUR_PROCESSO      
 DEALLOCATE CUR_PROCESSO      
 DELETE FROM LX_PROCESSO_LOG WHERE STATUS=1 AND DATA_PROCESSADO<=DATEADD(MONTH,-1,GETDATE())      
END      
      
      